<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字">
        <el-input clearable style="width: 240px" v-model="queryParams.search.keyword" placeholder="请输入姓名/手机号"
          clearable></el-input>
      </el-form-item>
      <el-form-item v-if="sysUser.isAdmin" label="加盟商">
        <el-select v-model="queryParams.search.franchiseeId" placeholder="请选择加盟商" clearable style="width: 240px">
          <el-option v-for="dict in franchiseeList" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.search.status" placeholder="司机状态" clearable style="width: 100%">
          <el-option v-for="dict in dict.type.sys_normal_disable" :key="Number(dict.value)" :label="dict.label"
            :value="Number(dict.value)" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:driver:list']">新增</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:driver:list']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:driver:list']">删除</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getDataList"></right-toolbar>
    </el-row>
    <el-table :data="dataList" v-loading="loading" @selection-change="handleSelectionChange" style="width: 100%;">
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="name" label="姓名" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="mobile" label="手机号" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="jobNumber" label="工号" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="createTime" label="创建时间" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>

      <el-table-column prop="drivingExperience" label="驾龄（年）" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" width="240" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-refresh-right" @click="resetPwd(scope.row)"
            v-hasPermi="['business:driver:list']">密码重置</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="preview(scope.row)"
            v-hasPermi="['business:driver:list']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:driver:list']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:driver:list']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getDataList" />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>



  </div>
</template>
<script>
  import {
    mapGetters
  } from 'vuex'
  import {
    page,
    deleteById,
    addOrUpdate,
    resetPwd
  } from "@/api/business/driverinfo";
  import {
    page as getOrder
  } from "@/api/business/orderinfo";
  import {
    page as getRecharge,
    getIncomes
  } from "@/api/business/driverrechargerecord";
  import AddOrUpdate from './addOrUpdate'
  export default {
    dicts: ['sys_normal_disable', 'sys_user_sex', 'order_status'],
    computed: {
      ...mapGetters(["sysUser"]),
    },
    data() {
      return {
        activeName: '1',
        loading: true,
        rechargeList: [{
          num: 12,
          time: '2022-10-10 10:00:00'
        }],
        franchiseeList: [],
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        // 总条数
        total: 0,
        //表格数据
        dataList: [],
        // 弹出层标题
        title: "",
        // 表单参数
        // 显示搜索条件
        showSearch: true,
        showInfo: false,
        driverInfo: {},
        form: {},
        // 日期范围
        dateRange: [],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          search: {
            keyword: '',
            franchiseeId: ''
          }
        },
        dataList: [],
        rules: {},
        addOrUpdateVisible: false,
        orderList: [],
        comerecordList: [],
        dataDate: []
      }
    },
    components: {
      AddOrUpdate
    },
    created() {
      this.getDataList();
      this.initPage();
    },
    methods: {
      changeDataType(e) {
        this.dataDate = [];
        if (e.name == '1') {
          this.getOrder();
        } else if (e.name == '2') {
          this.getRecharge();
        } else if (e.name == '3') {
          this.getIncomes();
        }
      },
      onDateChange(e) {
        if (this.activeName == '1') {
          this.getOrder();
        } else if (this.activeName == '2') {
          this.getRecharge();
        } else if (this.activeName == '3') {
          this.getIncomes();
        }
      },
      preview(e) {
        this.driverInfo = e;
        this.showInfo = true;
        this.activeName = '1'
        this.getOrder(e.id);
      },
      getOrder(e) {
        getOrder({
          pageNum: 0,
          pageSize: 100000,
          search: {
            driverId: this.driverInfo.id,
            startDate: this.dataDate[0],
            endDate: this.dataDate[1],
          }
        }).then(res => {
          this.orderList = res.data.list
        })
      },
      getIncomes(e) {
        getIncomes({
          driverId: this.driverInfo.id,
          startDate: this.dataDate[0],
          endDate: this.dataDate[1],
        }).then(res => {
          this.comerecordList = res.data
        })
      },
      getRecharge(e) {
        getRecharge({
          pageNum: 0,
          pageSize: 100000,
          search: {
            driverId: this.driverInfo.id,
            startDate: this.dataDate[0],
            endDate: this.dataDate[1],
          }
        }).then(res => {
          this.rechargeList = res.data.list
        })
      },
      initPage() {},
      // 获取数据列表
      getDataList() {
        this.loading = true;
        page(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.dataList = response.data.list;
          this.total = parseInt(response.data.total);
          this.loading = false;
        }).catch(res => {
          this.loading = false
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getDataList();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init({})
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.addOrUpdateVisible = true;
        this.title = "修改";
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row)
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('确定要删除选中的数据项？').then(function() {
          return deleteById(ids);
        }).then(() => {
          this.getDataList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },

      resetPwd(e) {
        this.$confirm('确定要重置当前司机的密码吗', '确认信息', {
          distinguishCancelAndClose: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(res => {
          resetPwd(e.id).then(info => {
            this.$message({
              message: '密码重置成功',
              type: 'success'
            });
          })
        })
      },
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          search: {
            keyword: '',
            franchiseeId: ''
          }
        };
        this.getDataList()
      }
    }
  }
</script>
<style scoped lang="scss">
  .driver_data {
    position: relative;

    .date {
      position: absolute;
      top: 0;
      right: 0;
      z-index: 1;
    }
  }

  .tab_name {
    font-size: 15px;
  }
</style>
